package com.msb.mall.ware.service.impl;

import com.msb.common.dto.SkuHasStockDTO;
import com.msb.common.dto.SkuReductionDTO;
import com.msb.common.utils.R;
import com.msb.mall.ware.feign.ProductFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.Query;

import com.msb.mall.ware.dao.WareSkuDao;
import com.msb.mall.ware.entity.WareSkuEntity;
import com.msb.mall.ware.service.WareSkuService;
import org.springframework.util.StringUtils;


@Service("wareSkuService")
public class WareSkuServiceImpl extends ServiceImpl<WareSkuDao, WareSkuEntity> implements WareSkuService {

    @Autowired
    private WareSkuDao wareSkuDao;

    @Autowired
    ProductFeignService productFeignService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        //查询条件
        QueryWrapper<WareSkuEntity> wrapper = new QueryWrapper<>();

        //skuId
        String skuId = (String) params.get("skuId");
        if (!StringUtils.isEmpty(skuId)) {
            wrapper.eq("sku_id", skuId);
        }

        //wareId
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            wrapper.eq("ware_id", wareId);
        }

        IPage<WareSkuEntity> page = this.page(
                new Query<WareSkuEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    /**
     * 采购项完成的入库操作
     *
     * @param skuId  商品编号
     * @param wareId 仓库编号
     * @param skuNum 采购数量
     */
    @Override
    public void addStock(Long skuId, Long wareId, Integer skuNum) {
        //判断是否有该商品和仓库的入库记录
        List<WareSkuEntity> wareSkuList = wareSkuDao.selectList(new QueryWrapper<WareSkuEntity>().eq("sku_id", skuId).eq("ware_id", wareId));
        if (wareSkuList == null || wareSkuList.size() == 0) {
            //没有
            //如果没有就新增商品库存记录
            WareSkuEntity wareSkuEntity = new WareSkuEntity();
            wareSkuEntity.setSkuId(skuId);
            wareSkuEntity.setWareId(wareId);
            wareSkuEntity.setStock(skuNum);
            wareSkuEntity.setStockLocked(0);
            //动态设置商品名称
            try {
                R info = productFeignService.info(skuId);   //远程调用商品服务接口
                Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
                if (info.getCode() == 0) {
                    wareSkuEntity.setSkuName((String) data.get("skuName"));
                }
            } catch (Exception e) {
                e.printStackTrace();
            }


            wareSkuDao.insert(wareSkuEntity);

        } else {
            //有
            //如果有就更新
            wareSkuDao.addStock(skuId, wareId, skuNum);
        }


    }

    /**
     * 获取每个sku对应的库存
     * @param skuIds
     * @return
     */
    @Override
    public List<SkuHasStockDTO> getSkusHasStock(List<Long> skuIds) {
        //select sum(stock - stock_locked) from wms_ware_sku where sku_id = #{skuIds}

        List<SkuHasStockDTO> list = skuIds.stream().map(skuId -> {
            Long count = baseMapper.getSkuStock(skuId);
            SkuHasStockDTO dto = new SkuHasStockDTO();
            dto.setSkuId(skuId);
            dto.setHasStock(count != null && count > 0);
            return dto;
        }).collect(Collectors.toList());
        return list;
    }

}